/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.support.dev.order.notify;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.yanlight.app.dal.dao.OrderDAO;
import com.yanlight.type.BooleanType;

/**
 * 
 * @author haolw
 * @time 2018/02/25
 */
class DevOrderStorageDispatcher extends DevAbstractDispatcher {
	private OrderDAO orderDAO;
	private final BlockingQueue<String> queue;

	public DevOrderStorageDispatcher() {
		queue = new LinkedBlockingQueue<String>(1024);
	}

	protected void addTask(String outTradeNo) {
		try {
			queue.offer(outTradeNo, 6, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void process() {
		try {
			while (true) {
				String outTradeNo = queue.take();
				doModify(outTradeNo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doModify(String outTradeNo) {
		orderDAO.updateNotifyStatus(outTradeNo, BooleanType.TRUE.ordinal());
	}

	public void setOrderDAO(OrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}
}
